package com.resou.reader.tinder.favorites;

import android.text.TextUtils;
import android.util.Log;

import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.Result;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.tinder.TinderRepository;
import com.resou.reader.data.tinder.tinder.CartBook;
import com.resou.reader.utils.ToastUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class FavoritesPresenter extends ResouBasePresenter<IFavoritesView> {
    private final TinderRepository mRepository;

    public FavoritesPresenter(IFavoritesView view) {
        super(view);
        mRepository = Injection.provideTinderRepository(ReSouApplication.getRSApplication());
    }

    public void setView(IFavoritesView view) {
        mView = view;
    }

    private static final String TAG = "FavoritesPresenter";

    /**
     * 获取收藏夹书本列表
     */
    public void getCartBookList(final String token) {
        if (TextUtils.isEmpty(token)) {
            ToastUtil.makeShortToast("请先登录");
            return;
        }
        mView.showProgress();
        addCompositeDisposable(
                mRepository.getCartBookListAll(token)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(resultList -> {
                            Log.d(TAG, "accept: ");
                            if (resultList.getCode() == 0) {
                                Log.d(TAG, "accept: resultList.size" + resultList.getData().size());
                                // 显示收藏列表
                                List<CartBook> data = resultList.getData();
                                if (data.size() == 0) {
                                    mView.showEmpty();
                                } else {
                                    mView.showContent(data);
                                }
                            } else {
                                ToastUtil.makeShortToast("服务器返回错误");
                            }
                        }, throwable -> {
//                                ErrorUtils.showError(throwable);
                            Log.d(TAG, "accept:xxx " + throwable.getMessage());
                            Log.d(TAG, "accept: " + throwable.getMessage());
                            mView.showError();
                        }));
    }

    /**
     * 从收藏夹删除此书
     */
    public void delete(String token, String bookId) {
        addCompositeDisposable(
                mRepository.delete(token, bookId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Result<String>>() {
                            @Override
                            public void accept(Result<String> stringResult) throws Exception {
                                // 校验删除的结果，似乎不太需要

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
//                                ErrorUtils.showError(throwable);
                                mView.showError();
                            }
                        }));
    }

}
